#!/bin/sh
echo $0 $*
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}

# GAMEDIR=${PWD}
GAMEDIR="/mnt/SDCARD/Ports/Abuse"
echo ==========GAMEDIR is $GAMEDIR
controlfolder="../PortMaster"

source $controlfolder/control.txt
#source $controlfolder/tasksetter

get_controls


exec > >(tee "$GAMEDIR/log.txt") 2>&1


$ESUDO chmod 666 /dev/tty1

cd $GAMEDIR

export DEVICE_ARCH="${DEVICE_ARCH:-aarch64}"

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GAMEDIR/libs:$GAMEDIR/gl4es.aarch64"
export SDL_GAMECONTROLLERCONFIG="$sdl_controllerconfig"

export LIBGL_ES=2
export LIBGL_GL=21
export LIBGL_FB=4
# export SDL_VIDEO_GL_DRIVER="$GAMEDIR/gl4es.aarch64/libGL.so.1"

GPTOKEYB_CONFIG="abuse.gptk"
sed -i '/ctr_left_stick_aim\=1/s//ctr_left_stick_aim\=0/' $GAMEDIR/user/config.txt

$ESUDO rm -rf ~/.abuse
ln -sfv $GAMEDIR/conf/.abuse ~/

$ESUDO chmod 666 /dev/uinput
$GPTOKEYB "abuse" -c "$GAMEDIR/$GPTOKEYB_CONFIG" &
./abuse
$ESUDO kill -9 $(pidof gptokeyb)
$ESUDO systemctl restart oga_events &
printf "\033c" > /dev/tty1